package authorized
import (
	"github.com/xinliangnote/go-gin-api/internal/pkg/sms"
	"github.com/xinliangnote/go-gin-api/configs"
	"strconv"
	"time"
)
func (s *serviceSt) SendLoginVerifyCode(phone_number string, verify_code string)(int32, error){
	cfg := configs.Get().Sms
	redis_key := cfg.LoginKeyPrefix + phone_number
	expire_time := cfg.LoginExpireTime
	//1. 发送验证码
	if client, err := sms.New(cfg.SecretId, cfg.SecretKey, cfg.SdkAppId, cfg.SignName); err!=nil{
		return -1, err
	}else{
		_, err = client.SendVerifyCode(phone_number, cfg.LoginAppTempId, verify_code, strconv.Itoa(expire_time))
		if err != nil{
			return -1, err
		}
	}

	//2. 如果发送成功，插入redis数据库，返回0,如果发送失败，返回-1
	
	if err := s.cache.Set(redis_key, verify_code, time.Second * time.Duration(expire_time)); err != nil {
		return -1, err
	}
	return 0, nil
}